package com.example.springboot.service.impl;

import com.example.springboot.dao.MenuMapper;
import com.example.springboot.domain.Menu;
import com.example.springboot.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;


    @Override
    public List<Menu> findSubMenuListByPid(int pid) {
        List<Menu> subMenuListByPid = menuMapper.findSubMenuListByPid(pid);
        return subMenuListByPid;
    }

    @Override
    public List<Menu> findAllMenu() {
        List<Menu> allMenu = menuMapper.findAllMenu();
        return allMenu;
    }

    @Override
    public Menu findMenuById(Integer id) {

        return menuMapper.findMenuById(id);
    }

    @Override
    public void updateMenu(Menu menu) {
        Date date = new Date();
        menu.setUpdatedTime(date);
        menu.setCreatedBy("system");
        menu.setUpdatedBy("system");
        menuMapper.updateMenu(menu);
    }

    @Override
    public void addMenu(Menu menu) {

        Date date = new Date();
        menu.setUpdatedTime(date);
        menu.setCreatedTime(date);
        menu.setCreatedBy("system");
        menu.setUpdatedBy("system");
        menuMapper.addMenu(menu);
    }

    @Override
    public void deleteMenu(Integer id) {
        //先在role_menu_relation删除关于这个菜单的数据
        menuMapper.deleteMenuAndRoleRelation(id);
        //再在menu删除这个菜单
        menuMapper.deleteMenu(id);
    }
}
